package com.bub.pay.framework.common.util;


import com.bub.pay.framework.common.base.Cause;
import com.bub.pay.framework.common.exception.GlobalException;
import org.apache.commons.lang3.StringUtils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;

public interface StrUtil {

    String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    String ALLNUMBER = "0123456789";

    /**
     * 得到随机序列，主要用于随机生成长度是16-32位字符串
     *
     * @return
     */
    static String getRandomsequence() {
        SecureRandom r = new SecureRandom();
        int l = 16 + r.nextInt(17);// 设置随机数长度为16-32
        return getSequence(l);
    }

    /**
     * 得到指定长度的随机序列
     *
     * @return
     */
    static String getSequence(Integer length) {
        SecureRandom r = new SecureRandom();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(r.nextInt(ALLCHAR.length())));
        }
        return sb.toString();
    }


    /**
     * @param digit 格式化字符长度
     * @return
     */
    static String getFormater(Integer digit) {
        String s = "%0" + digit + "d";
        return s;
    }

    static boolean checkParamByPattern(String pattern, Object obj) {
        if (obj == null) {
            return false;
        }
        return Pattern.compile(pattern).matcher(obj.toString()).matches();
    }

    /**
     *
     * @param str 原字符串
     * @param start 前面保留几位字符
     * @param end  后面保留几位字符
     * @return
     */
    static String hideCenterNumber(String str,int start,int end){
        if (StringUtils.isBlank(str)){
            return "";
        }
        StringBuffer sb = new StringBuffer();
        String code = "********************************";
        int length = str.length();
        if (length <= start+end){
            return str;
        }

        String s1 = str.substring(0, start);
        String s2 = str.substring(length - end, length);
        sb.append(s1);
        if (code.length() > length - start - end){
            code = code.substring(0, length - start - end);
        }
        sb.append(code);
        sb.append(s2);

        return sb.toString();
    }
    /**
     * @description 谷歌验证码类型转化
     * @param code 谷歌验证码
     * @return
     */
    static Long getGoogleCode(String code) {
        Long googleCode;
        try {
            googleCode = Long.parseLong(code);
        } catch (Exception e) {
            throw new GlobalException(Cause.error_google_secret_code);
        }
        return googleCode;
    }

     static String uuid20(String prefix) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        try {
            int randomMath = (int)((Math.random()*9+1)*1000);
            return prefix + simpleDateFormat.format(new Date(System.currentTimeMillis())) + randomMath;
        } catch (Exception e) {
            Random random = new Random(System.currentTimeMillis());
            String tmp = "";
            for (int i = 0; i < 18; i++) {
                tmp += random.nextInt(10);
            }
            return simpleDateFormat.format(new Date(System.currentTimeMillis())) + tmp;
        }
    }


     static String uuid8(String prefix) {
        try {
            return prefix + String.valueOf(_IdWorker.getFlowIdWorkerInstance().nextId()).substring(0, 8);
        } catch (Exception e) {
            Random random = new Random(System.currentTimeMillis());
            String tmp = "";
            for (int i = 0; i < 8; i++) {
                tmp += random.nextInt(10);
            }
            return tmp;
        }
    }


     static String uuid16(String prefix) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            return prefix + simpleDateFormat.format(new Date(System.currentTimeMillis())).substring(2, 8) + String.valueOf(_IdWorker.getFlowIdWorkerInstance().nextId()).substring(0, 10);
        } catch (Exception e) {
            Random random = new Random(System.currentTimeMillis());
            String tmp = "";
            for (int i = 0; i < 16; i++) {
                tmp += random.nextInt(10);
            }
            return simpleDateFormat.format(new Date(System.currentTimeMillis())).substring(2, 8) + tmp.substring(0, 10);
        }
    }

    /**生成8位以上不重复随机数*/
     static String GetRandomString(int Len) {
        String[] baseString={"0","1","2","3",
                "4","5","6","7","8","9",
                "a","b","c","d","e",
                "f","g","h","i","j",
                "k","l","m","n","o",
                "p","q","r","s","t",
                "u","v","w","x","y",
                "z","A","B","C","D",
                "E","F","G","H","I",
                "J","K","L","M","N",
                "O","P","Q","R","S",
                "T","U","V","W","X","Y","Z"};
        Random random = new Random();
        int length=baseString.length;
        String randomString="";
        for(int i=0;i<length;i++){
            randomString+=baseString[random.nextInt(length)];
        }
        random = new Random(System.currentTimeMillis());
        String resultStr="";
        for (int i = 0; i < Len; i++) {
            resultStr += randomString.charAt(random.nextInt(randomString.length()-1));
        }
        return resultStr;
    }

    static String mapToString(Map<String, Object> dataMap) {
        StringBuilder sb = new StringBuilder();
        for (String key : dataMap.keySet()) {
            Object value = dataMap.get(key);
            sb.append(key).append("=").append(value).append("&");
        }
        System.out.println(sb.toString());
        return sb.substring(0, sb.length() - 1);
    }
}
